package com.flashcash.kotlin.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import com.flashcash.solu.R
import kotlinx.android.synthetic.main.dialog_date_picker.*
import java.util.*

class DatePickerDialog : Dialog {
    private var mode = DatePickerMode.MODE_YM

    private var title = "please choose date"

    private var onDatePickerListener: OnDatePickerListener?

    private val monthArray = arrayOf(
            String.format("%02d", Month.JANUARY.ordinal + 1),
            String.format("%02d", Month.FEBRUARY.ordinal + 1),
            String.format("%02d", Month.MARCH.ordinal + 1),
            String.format("%02d", Month.APRIL.ordinal + 1),
            String.format("%02d", Month.MAY.ordinal + 1),
            String.format("%02d", Month.JUNE.ordinal + 1),
            String.format("%02d", Month.JULY.ordinal + 1),
            String.format("%02d", Month.AUGUST.ordinal + 1),
            String.format("%02d", Month.SEPTEMBER.ordinal + 1),
            String.format("%02d", Month.OCTOBER.ordinal + 1),
            String.format("%02d", Month.NOVEMBER.ordinal + 1),
            String.format("%02d", Month.DECEMBER.ordinal + 1))

    constructor(context: Context, onDatePickerListener: OnDatePickerListener?) : super(context) {
        this.onDatePickerListener = onDatePickerListener
    }

    constructor(context: Context, title: String, mode: DatePickerMode, onDatePickerListener: OnDatePickerListener?) : super(context) {
        this.onDatePickerListener = onDatePickerListener
        this.mode = mode
        this.title = title
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_date_picker)

        text_view_title.text = title

        number_picker_month.displayedValues = monthArray
        number_picker_month.maxValue = monthArray.size - 1
        number_picker_month.minValue = 0
        number_picker_month.value = Calendar.getInstance().get(Calendar.MONTH)

        number_picker_year.maxValue = Calendar.getInstance().get(Calendar.YEAR) + 200
        number_picker_year.minValue = Calendar.getInstance().get(Calendar.YEAR) - 30
        number_picker_year.value = Calendar.getInstance().get(Calendar.YEAR)

        setCanceledOnTouchOutside(true)

        button_cancel.setOnClickListener { dismiss() }

        when (mode) {
            DatePickerMode.MODE_YM -> {
                button_confirm.setOnClickListener {
                    val year = String.format("%d", number_picker_year.value)
                    val month = String.format("%s", monthArray[number_picker_month.value])

                    onDatePickerListener?.onDatePicker(year, month)

                    dismiss()
                }
            }
        }
    }

    interface OnDatePickerListener {
        fun onDatePicker(year: String, month: String)
    }

    enum class DatePickerMode {
        MODE_YM, // year and month
    }

    enum class Month {
        JANUARY,
        FEBRUARY,
        MARCH,
        APRIL,
        MAY,
        JUNE,
        JULY,
        AUGUST,
        SEPTEMBER,
        OCTOBER,
        NOVEMBER,
        DECEMBER,
    }
}